// pages/songDetail/songDetail.js
import request from '../../utils/request.js'
import PubSub from 'pubsub-js'
import moment from 'moment'
const appInstance  = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		isPlay: false,
		song: {},
		musicId: '',
		musicLink: '',
		currentTime: '',
		durationTime: '',
		currentWidth:0,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		//options用来接收路由跳转传递参数
		//原生小程序中路由传参对参数长度有限制，如果参数长度过长会自动截取掉
		let currentMusicId = options.musicId
		this.setData({
			musicId: currentMusicId
		})
		this.getMusicInfo(currentMusicId)
		
		//判断当前音乐是否再播放
		if (appInstance.globalData.isMusicPlay && appInstance.globalData.musicId === currentMusicId) {
			this.setData({
				isPlay: true
			})
		}
		
		this.backgroundAudioManager = wx.getBackgroundAudioManager()
		//通过控制音频实例来去监听音乐播放、暂停（因为系统栏的暂停播放按钮小程序页面不知道）
		this.backgroundAudioManager.onPlay(() => {
			// this.setData({
			// 	isPlay: true
			// })
			this.changeMusicState(true)
			
			//修改全局音乐播放状态
			appInstance.globalData.isMusicPlay = true
			appInstance.globalData.musicId = currentMusicId
		})
		this.backgroundAudioManager.onPause(() => {
			// this.setData({
			// 	isPlay: false
			// })
			this.changeMusicState(false)
			
		})
		//监听音乐停止
		this.backgroundAudioManager.onStop(() => {
			// this.setData({
			// 	isPlay: false
			// })
			this.changeMusicState(false)
		
		})
		
		//监听音乐播放进度
		this.backgroundAudioManager.onTimeUpdate(() => {
			// console.log(this.backgroundAudioManager.currentTime);
			let currentTime = moment(this.backgroundAudioManager.currentTime * 1000).format('mm:ss')
			let currentWidth = this.backgroundAudioManager.currentTime/this.backgroundAudioManager.duration * 450
			this.setData({
				currentTime,
				currentWidth
			})
		})
		//监听音乐播放自然结束
		this.backgroundAudioManager.onEnded(() => {
			//播放完成后切换至下一首
			PubSub.publish('switchType','next')
			this.setData({
				currentWidth: 0,
				currentTime: '00:00'
			})
		})
	},
	//修改音乐播放状态
	changeMusicState(isPlay) {
		this.setData({
			isPlay: isPlay
		})
		appInstance.globalData.isMusicPlay = true
	},
	//点击播放暂停回调
	handleMusicPlay() {
		let isPlay = !this.data.isPlay
		// this.setData({
		// 	isPlay
		// })
		let {musicId,musicLink} = this.data
		this.musicControl(isPlay,musicId,musicLink)
	},
	//控制音乐播放暂停功能
	async musicControl(isPlay,musicId,musicLink) {
		// console.log(isPlay,musicId,musicLink);
		if (isPlay) {
			if (!musicLink) {
				//获取音乐播放链接
				let res = await request('/song/url',{id: musicId})
				let musicLink = res.data[0].url
				this.setData({
					musicLink
				})
				//创建控制播放音频实例
				this.backgroundAudioManager.src = musicLink
			}
			
			this.backgroundAudioManager.title = this.data.song.name
		}else {
			this.backgroundAudioManager.pause()
		}
	},
	
	//获取音乐详情
	async getMusicInfo(musicId) {
		let res = await request('/song/detail',{ids: musicId})
		let durationTime = moment(res.songs[0].dt).format('mm:ss')
		this.setData({
			song: res.songs[0],
			durationTime
		})
		//动态修改窗口标题
		wx.setNavigationBarTitle({
			title: this.data.song.name
		})
	},
	
	//切换歌曲回调
	handleSwitch(e) {
		let type = e.currentTarget.id
		this.backgroundAudioManager.stop()
		PubSub.subscribe('musicId',(msg,musicId) => {
			this.getMusicInfo(musicId)
			this.musicControl(true,musicId)
			PubSub.unsubscribe('musicId')
		})
		PubSub.publish('switchType',type)
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
